package com.uh.calculation.inter.imp;

import com.alibaba.fastjson.JSONObject;
import com.uh.calculation.bean.OperatorVal;
import com.uh.calculation.bean.WarnJiSuanBean;
import com.uh.calculation.inter.Warn;
import com.uh.calculation.operator.*;
import com.uh.calculation.util.GetOrganization;
import com.uh.microservice.core.dao.JdbcDao;
import com.uh.summary.enums.RuleEnum;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class S_MaoyiImp implements Warn {



    private JdbcDao jdbcDao;

    public WarnJiSuanBean warnJiSuan = null;

    List<Map<String, Object>> S204ls = null;
    List<Map<String, Object>> S203ls = null;
    List<Map<String, Object>> S204_1ls = null;
    List<Map<String, Object>> S104ls = null;
    List<Map<String, Object>> HJ_ls = null;
    List<Map<String, Object>> HJN_ls = null;
    List<Map<String, Object>> F201_1ls = null;


    //1-本月 营业收入修改比例超过15%，计预警因子50。
    //年报营业收入修改比例超过15%，计预警因子50。（此条仅用于4月年报出来时，但一直触发到该企业被核查或检查为止）
    private String  xgfd="[{dimension:\"A\",field:\"$NEW_VALUE;$NEW_VALUE#m\",formulaBeans:[{formula:\"($NEW_VALUE@$NEW_VALUE#m)/$NEW_VALUE#m>0.15|50\"}]}]";

    private String  xgcs="[{dimension:\"A\",field:\"$NEW_VALUE\",formulaBeans:[{formula:\"$NEW_VALUE>3|50\"}]}]";


    //每季末的1-本月营业额(s204-1 M6)/季报上的营业收入(e203 M26)为115%时，计100分，每比115%大一个百分点，加2分。为85%时，计100分，每小85%一个百分点，加2分。（该条只会在每季季报出来后才会触发，一旦触发，持续到该企业被核查或检查）
    private String pp2 = "[{dimension:\"E\",field:\"S204-1_M6;S203_M26\",formulaBeans:[{formula:\"S204-1_M6/S203_M26>=1.15&((((S204-1_M6/S203_M26)-1.15)*100)*2)+100>=200|200\"},{formula:\"S204-1_M6/S203_M26>=1.15&((((S204-1_M6/S203_M26)-1.15)*100)*2)+100<200|((((S204-1_M6/S203_M26)-1.15)*100)*2)+100\"},{formula:\"S204-1_M6/S203_M26<=0.85&((((S204-1_M6/S203_M26)@0.85)*100)*2)+100>=200|200\"},{formula:\"S204-1_M6/S203_M26<=0.85&((((S204-1_M6/S203_M26)@0.85)*100)*2)+100<200|((((S204-1_M6/S203_M26)@0.85)*100)*2)+100\"}]}]";


    //贸易-住餐
    private String zc = "[{dimension:\"B\",field:\"S104-1_M1\",formulaBeans:[{formula:\"E104-1_M<2400|50\"}]}]";


    @Override
    public Integer jisuan(WarnJiSuanBean warnJiSuan) {
        this.warnJiSuan = warnJiSuan;
        this.jdbcDao = warnJiSuan.getJdbcDao();
        GetOrganization getOrganization = new GetOrganization(jdbcDao);

        Date lastTime = warnJiSuan.getLastTime();
        S204ls = getOrganization.getFieldName("S204-1", warnJiSuan.getYear(), warnJiSuan.getSurveyobjectid());
        S203ls = getOrganization.getFieldName("S203", warnJiSuan.getYear(), warnJiSuan.getSurveyobjectid());
        S204_1ls = getOrganization.getFieldName_TJRY("S204-1", warnJiSuan.getYear(), warnJiSuan.getSurveyobjectid(), lastTime);

        HJ_ls =  getOrganization.gethj("et_8600000002021110107720330", warnJiSuan.getSurveyobjectid());
        //年报
        HJN_ls =  getOrganization.gethj("et_8600000002021110107720748", warnJiSuan.getSurveyobjectid());
        F201_1ls = getOrganization.getFieldName("201-1", warnJiSuan.getYear(), warnJiSuan.getSurveyobjectid());

        warnJiSuan.setF201_1ls(F201_1ls);


        HjOperator hjOperator = new HjOperator(jdbcDao, warnJiSuan);
        List<OperatorVal> list = JSONObject.parseArray(xgfd, OperatorVal.class);
        //修改一
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.TRADE_EDIT_FREQUENCY_FACTOR_MONTH_DATA.getCode());//风险因子类型
        hjOperator.jisuan(HJ_ls, list,"本年;本月;商品销售额;千元|本年;1-本月;商品销售额;千元|本年;本月;零售额;千元|本年;1-本月;零售额;千元|本年;1-本月;零售额;千元");
        //hjOperator.jisuan(HJ_ls, list,"本年;1-本月;商品销售额;千元");
        //hjOperator.jisuan(HJ_ls, list,"本年;本月;零售额;千元");
        //hjOperator.jisuan(HJ_ls, list,"本年;1-本月;零售额;千元");
        //hjOperator.jisuan(HJ_ls, list,"本年;1-本月;零售额;千元");

        warnJiSuan.setIs_hc(true);
        hjOperator.jisuan(HJN_ls, list,"本年;商品销售额;零售额;千元|本年;服务营业额;千元");
        //hjOperator.jisuan(HJN_ls, list,"本年;服务营业额;千元");




        list = JSONObject.parseArray(xgcs, OperatorVal.class);
        HjNumOperator hjNumOperator = new HjNumOperator(jdbcDao, warnJiSuan);
        warnJiSuan.setWarnType(RuleEnum.TRADE_EDIT_FREQUENCY_FACTOR_MONTH_DATA.getCode());//风险因子类型
        warnJiSuan.setIs_hc(false);
        hjNumOperator.jisuan(HJ_ls, list,"本年;本月;商品销售额;千元");
        hjNumOperator.jisuan(HJ_ls, list,"本年;1-本月;商品销售额;千元");
        hjNumOperator.jisuan(HJ_ls, list,"本年;本月;零售额;千元");
        hjNumOperator.jisuan(HJ_ls, list,"本年;1-本月;零售额;千元");

        warnJiSuan.setIs_hc(true);
        hjNumOperator.jisuan(HJ_ls, list,"本年;商品销售额;零售额;千元");
        hjNumOperator.jisuan(HJ_ls, list,"本年;服务营业额;千元");
        hjNumOperator.jisuan(HJ_ls, list,"本年;商品销售额;批发额;千元");
        hjNumOperator.jisuan(HJ_ls, list,"本年;商品销售额;批发额;千元");
        //




        //启动季报与月报匹配算子
        JtYOperator jtYOperator = new JtYOperator(jdbcDao, warnJiSuan);
        list = JSONObject.parseArray(pp2, OperatorVal.class);


        //匹配二
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.TRADE_MATCH_FACTOR_DATA.getCode());//风险因子类型
        jtYOperator.jisuan(S203ls, S204ls, list);


        TjryOperator tjryOperator = new TjryOperator(jdbcDao, warnJiSuan);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.TRADE_ABNORMAL_STATISTICS_FACTOR_DATA.getCode());//风险因子类型
        tjryOperator.jisuan(S204_1ls, "M5", "B");


        //小升规  贸易-住餐
        XsgOperator xsgOperator = new XsgOperator(jdbcDao, warnJiSuan);
        list = JSONObject.parseArray(zc, OperatorVal.class);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.TRADE_ABNORMAL_SMALL_FACTOR_DATA2.getCode());//风险因子类型
        xsgOperator.jisuanMaoyiZc(S104ls, list);





        return  null;
    }
}
